/*
 *  Copyright (C) 2010 MADSA.
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/. 
 *
 *  Author: Victor Mayoral Vilches
 *  
 */

package com.madsa.tuPeli;

import java.util.ArrayList;

import com.sisof.R;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TuPeliActivity extends ListActivity {

	private ProgressDialog m_ProgressDialog = null;
	private ArrayList<Cine> m_orders = null;
	private CineAdapter m_adapter;
	private Runnable viewOrders;
	private String peticion;

	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tu_peli_activity);
						
		//Obtain the extra information given from the parent
		Bundle extras  = getIntent().getExtras();
		CharSequence titulo;
		if(extras != null){
			titulo = extras.getCharSequence("pelicula");
			peticion= "Resultados: " + titulo;
			
		}else{
			peticion= "Error en la obtención de la película introducida";
		}        

		setTitle(peticion);
		
		m_orders = new ArrayList<Cine>();
		this.m_adapter = new CineAdapter(this,R.layout.row , m_orders);
		setListAdapter(this.m_adapter);

		viewOrders = new Runnable() {
			public void run() {
				getOrders();
			}
		};
		
		Thread thread = new Thread(null, viewOrders, "LoadingBackground");
		thread.start();
		m_ProgressDialog = ProgressDialog.show(TuPeliActivity.this,
				"Por favor espere...", "Cargando datos...", true);
	}

	private Runnable returnRes = new Runnable() {
		public void run() {
			if (m_orders != null && m_orders.size() > 0) {
				m_adapter.notifyDataSetChanged();
				for (int i = 0; i < m_orders.size(); i++)
					m_adapter.add(m_orders.get(i));
			}
			m_ProgressDialog.dismiss();
			m_adapter.notifyDataSetChanged();
		}
	};


	private void getOrders() {
		try {
			m_orders = new ArrayList<Cine>();
			m_orders.add(new Cine("Cinesa ParqueSur","7.20 €"));
			m_orders.add(new Cine("Cinesa Loranca","6.20 €"));
			m_orders.add(new Cine("Cinesa Abaco Boulevard","9.20 €"));
			m_orders.add(new Cine("Cinesa URJC","2.20 €"));
			m_orders.add(new Cine("Cinesa Abaco Boulevard","9.20 €"));
			m_orders.add(new Cine("Cinesa URJC","2.20 €"));
			Thread.sleep(5000);  //simulate the connection with the server.
			Log.i("ARRAY", "" + m_orders.size());
		} catch (Exception e) {
			Log.e("BACKGROUND_PROC", e.getMessage());
		}
		runOnUiThread(returnRes);
	}

	private class CineAdapter extends ArrayAdapter<Cine> {
		private ArrayList<Cine> items;

		public CineAdapter(Context context, int textViewResourceId,
				ArrayList<Cine> items) {
			super(context, textViewResourceId, items);
			this.items = items;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
			if (v == null) {
				LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = vi.inflate(R.layout.row, null);
			}
			Cine o = items.get(position);
			if (o != null) {
				TextView tt = (TextView) v.findViewById(R.id.toptext);
				TextView bt = (TextView) v.findViewById(R.id.bottomtext);
				if (tt != null) {
					tt.setText("Nombre: " + o.getName());
				}
				if (bt != null) {
					bt.setText("Precio: " + o.getName());
				}
			}
			return v;
		}
	}
}